using System;
using ConnectSQLServer.Controls;
using System.Collections.Generic;
using Crownwood.Magic.Controls;
using Crownwood.Magic.Common;
using Crownwood.Magic.Win32;
using Crownwood.Magic.Menus;
using ConnectSQLServer.Common;

namespace ConnectSQLServer.Manager
{
    /// <summary>
    /// Summary description for TabbedWindowManager.
    /// </summary>
    public class TabControlManager
    {
        private TabControl TC;
        private List<int> lsSelectedIndex;
        private int iSelectedIndex;

        public TabControlManager(TabControl tabControl)
        {
            TC = tabControl;
            TC.Appearance = Crownwood.Magic.Controls.TabControl.VisualAppearance.MultiDocument;
            TC.ShowArrows = true;
            lsSelectedIndex = new List<int>();
            iSelectedIndex = 0;
            TC.ClosePressed += new EventHandler(RemoveTabPage);
            TC.SizeChanged += new EventHandler(SizeChanged);

        }

        public void SizeChanged(object sender, EventArgs e)
        {
            foreach (TabPage tabpage in TC.TabPages)
            {
                if (tabpage.Controls[0].Height == GlobalVariable.heightPanel2)
                    HideResurlControl(tabpage);
                else
                    ShowResurlControl(tabpage);
            }

        }
        public TabPage GetTabPageSelected()
        {
            if (TC.TabPages.Count > 0)
                return TC.TabPages[TC.SelectedIndex];
            else
                return null;
        }
        public void AddTabPage(TabPage tabPage)
        {
            TC.TabPages.Add(tabPage);
            iSelectedIndex = TC.TabPages.Count - 1;
            TC.TabPages[iSelectedIndex].Selected = true;
            lsSelectedIndex.Add(iSelectedIndex);
        }

        public void RemoveTabPage(object sender, EventArgs e)
        {
            if (TC.TabPages.Count == 1)
            {
                lsSelectedIndex.Clear();
                iSelectedIndex = 0;
                TC.TabPages.Clear();
            }
            if (TC.TabPages.Count > 0 && TC.TabPages.Count != 1)
            {
                TC.TabPages.RemoveAt(this.iSelectedIndex);
                lsSelectedIndex.RemoveAt(lsSelectedIndex.Count - 1);
                iSelectedIndex = lsSelectedIndex.Count - 1;
                TC.TabPages[iSelectedIndex].Selected = true;
            }
        }

        public void ShowResurlControl(TabPage tp)
        {
            tp.Controls[0].Location = new System.Drawing.Point(0, 0);
            tp.Controls[0].Size = new System.Drawing.Size(GlobalVariable.widthPanel2, GlobalVariable.heightPanel2 / 2);
            tp.Controls[1].Location = new System.Drawing.Point(0, tp.Controls[0].Height);
            tp.Controls[1].Size = new System.Drawing.Size(GlobalVariable.widthPanel2, GlobalVariable.heightPanel2 - tp.Controls[0].Height);

        }
        public void HideResurlControl(TabPage tp)
        {
            tp.Controls[0].Location = new System.Drawing.Point(0, 0);
            tp.Controls[0].Size = new System.Drawing.Size(GlobalVariable.widthPanel2, GlobalVariable.heightPanel2 - GlobalVariable.padding);
            tp.Controls[1].Size = new System.Drawing.Size(0, 0);

        }
    }
}
